package com.example.websocket.controller;

import com.example.websocket.server.WebSocketServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;


/**
 * Created by liulanhua on 2018/9/25.
 */
@Slf4j
@RestController
public class TestController {


    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView index(ModelAndView mv) throws Exception {
        mv.addObject("record", "this is index page");
        mv.setViewName("index");
        return mv;
    }


    @GetMapping("/websocket")
    public ModelAndView websocket() {
        log.info(">>>websocket page");
        return new ModelAndView("/websocket");
    }

    @RequestMapping("/push/{toUserId}")
    public ResponseEntity<String> pushToWeb(String message, @PathVariable String toUserId, HttpServletRequest request)
            throws IOException {
        log.info(">>>request url:{}, sessionId:{}, message:{}", "/push", request.getSession().getId(), message);
        WebSocketServer.sendInfo(message, toUserId);
        return ResponseEntity.ok(">>>MSG SEND SUCCESS");
    }


}
